javascript:
(function(){
	var lootrows = document.getElementsByClassName('main_table')[0].getElementsByTagName('tr');
	var regexloot = /(alt="(.*?)".*?(\d+ Attack).*?(\d+ Defense).*?Owned: (\d+))|(alt="(.*?)").*?(Owned: \d+)/;
	var regexwish = /wishlist_(add|remove).*?gift_category=(\d+).*gift_id=(\d+)/;
	var regextype = /<span class="text">(Weapons|Armor|Vehicles|Animals)<\/span>/;
	var head = '';
	var body = '';
	var tail = '';
	var weapons = [];
	var armor = [];
	var vehicles = [];
	var animals = [];
	var type = 'unknown';

	function lootObject(name,attack,defense,amount,type,category,id) {
		this.name = name;
		this.attack = parseInt(attack);
		this.defense = parseInt(defense);
		this.combined = parseInt(this.attack+this.defense);
		this.amount = parseInt(amount);
		this.type = type;
		if (category && id) {
			this.wishlist = '<a href="http://apps.facebook.com/inthemafia/track.php?next_controller=loot&next_action=wishlist_add&next_params=%7B%22gift_category%22%3A%22'+category+'%22%2C%22gift_id%22%3A%22'+id+'%22%7D">Add to Wishlist</a>';
			//this.wishlist = '<a href="http://apps.facebook.com/inthemafia/track.php?next_controller=loot&next_action=wishlist_add&next_params=%7B%22gift_category%22%3A%22'+category+'%22%2C%22gift_id%22%3A%22'+id+'%22%7D" onclick="return do_ajax(\'inner_page\', \'remote/html_server.php?xw_controller=loot&xw_action=wishlist_add&xw_city=&tmp=&cb=&xw_person=&gift_category='+category+'&gift_id='+id+', 1, 1, 0, 0\'); return false;">Add to Wishlist</a>';
		}
		else {
			this.wishlist = '';
		}
	}

	function writeTable(object,sortby,header,limit) {
		if (sortby == 'attack') {
			object.sort(function(a,b) { return parseInt(b.attack) - parseInt(a.attack) } );
		}
		if (sortby == 'defense') {
			object.sort(function(a,b) { return parseInt(b.defense) - parseInt(a.defense) } );
		}
		if (sortby == 'combined') {
			object.sort(function(a,b) { return parseInt(b.combined) - parseInt(a.combined) } );
		}
		var count = 0;
		body += '<table class="messages">\n';
		body += '<tr><th><h2>Top '+header+'</h2></th><th>Attack:</th><th>Defense:</th>'+((sortby == 'combined')?'<th>Combined:</th>':'')+'<th>Using:</th><th>Giftable:</th></tr>';
		for (x in object) {
			body += '<tr><td>'+object[x].name+'</td><td>'+object[x].attack+'</td><td>'+object[x].defense+'</td>'+((sortby == 'combined')?'<td>'+object[x].combined+'</td>':'')+'<td>'+((count+object[x].amount)>limit?limit-count:object[x].amount)+'/'+object[x].amount+'</td><td>'+object[x].wishlist+'</td></tr>\n';
			count += parseInt(object[x].amount);
			if (count > limit) {
				break;
			}
		}
		body += '</table>';
	}
	
	for (i=0;i<lootrows.length;i++) {
		var rowcontent = lootrows[i].innerHTML.replace(/[\n\r\t]/g,'');
		if (regextype.test(rowcontent)) {
			type = regextype.exec(rowcontent)[1];
		}
		if (regexloot.test(rowcontent)) {
			var item = regexloot.exec(rowcontent);
			if (item[2]) {
				if (regexwish.test(rowcontent)) {
					var wishlist = regexwish.exec(rowcontent);
					if (type == 'Weapons') {
						weapons[weapons.length] = new lootObject(item[2],item[3],item[4],item[5],type,wishlist[2],wishlist[3]);				
					}
					if (type == 'Armor') {
						armor[armor.length] = new lootObject(item[2],item[3],item[4],item[5],type,wishlist[2],wishlist[3]);				
					}
					if (type == 'Vehicles') {
						vehicles[vehicles.length] = new lootObject(item[2],item[3],item[4],item[5],type,wishlist[2],wishlist[3]);				
					}
					if (type == 'Animals') {
						animals[animals.length] = new lootObject(item[2],item[3],item[4],item[5],type,wishlist[2],wishlist[3]);				
					}
				}
				else {
					if (type == 'Weapons') {
						weapons[weapons.length] = new lootObject(item[2],item[3],item[4],item[5],type,false,false);				
					}
					if (type == 'Armor') {
						armor[armor.length] = new lootObject(item[2],item[3],item[4],item[5],type,false,false);				
					}
					if (type == 'Vehicles') {
						vehicles[vehicles.length] = new lootObject(item[2],item[3],item[4],item[5],type,false,false);				
					}
					if (type == 'Animals') {
						animals[animals.length] = new lootObject(item[2],item[3],item[4],item[5],type,false,false);				
					}
				}
			}
		}
	}
	
	//var attack = weapons.sort(function(a,b) { return parseInt(b.attack) - parseInt(a.attack) } );
	//var defense = weapons.sort(function(a,b) { return parseInt(b.defense) - parseInt(a.defense) } );

	head += '<div id="spockloot">\n';
	head += '<h2>Top <input type="text" id="mafiasize" value="501" size="9"> Attack and Defense items</h2>';
	head += '<p>Sort by: <a href="#" id="attack">Attack</a> | <a href="#" id="defense">Defense</a> | <a href="#" id="combined">Combined</a> | <a href="#" onclick="javascript:$(\'#spockloot\').remove();return false;">Remove window</a> | <a href="http://www.spockholm.com/mafia/testing.php">Spockholm Mafia Tools</a></p>\n';
	body += '<div id="showloot"></div>';
	tail += '</div>\n';
	$('#spockloot').remove();
	$('.main_table').eq(0).before(head+body+tail);
	
	
	document.getElementById('attack').onclick=sortByAttack;
	function sortByAttack() {
		body = '';
		var mafia = parseInt($("#mafiasize").val());
		writeTable(weapons,'attack','Weapons',mafia);
		writeTable(armor,'attack','Armor',mafia);
		writeTable(vehicles,'attack','Vehicles',mafia);
		writeTable(animals,'attack','Animals',mafia);
		$('#showloot').html(body);
		return false;
	};
	document.getElementById('defense').onclick=function() {
		body = '';
		var mafia = parseInt($("#mafiasize").val());
		writeTable(weapons,'defense','Weapons',mafia);
		writeTable(armor,'defense','Armor',mafia);
		writeTable(vehicles,'defense','Vehicles',mafia);
		writeTable(animals,'defense','Animals',mafia);
		$('#showloot').html(body);
		return false;
	};
	document.getElementById('combined').onclick=function() {
		body = '';
		var mafia = parseInt($("#mafiasize").val());
		writeTable(weapons,'combined','Weapons',mafia);
		writeTable(armor,'combined','Armor',mafia);
		writeTable(vehicles,'combined','Vehicles',mafia);
		writeTable(animals,'combined','Animals',mafia);
		$('#showloot').html(body);
		return false;
	};	
	sortByAttack();
	
	
}
)()